package com.fg.cloud.factory.controller.product;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.product.ProductSpecKeyEntity;
import com.fg.cloud.manager.entry.product.ProductSpecModelEntity;
import com.fg.cloud.manager.entry.product.vo.ProductSpecModelVo;
import com.fg.cloud.manager.service.product.ProductSpecModelService;

/**
  * @Description:商品规格模板controller
  * @author dingjiang
  * @date 2017年8月7日下午2:42:38
*/
@Controller
@RequestMapping(value="/spec")
public class ProductSpecModelController extends BaseController{

	@Autowired
	private ProductSpecModelService specModelService;
	
	/**
	 * @Description: 新增规格模板、规格名、规格值
	 * @param req
	 * @param resp
	 * @param specModelEntity 规格模板实体
	 * @author dingjiang
	 * @date 2017年8月7日下午2:42:38
	 * @throws 
	 */
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert( HttpServletRequest req , HttpServletResponse resp , String specModel ){
		try {
			ProductSpecModelEntity specModelEntity = JSON.parseObject(StringEscapeUtils.unescapeHtml4(specModel), ProductSpecModelEntity.class);
			List<ProductSpecKeyEntity> keys = specModelEntity.getSpecKeyEntities();
			/*System.out.println(specModelEntity.getModelName()+"--"+keys);*/
			//验证非空
			if ( null == specModelEntity.getModelName() || StringUtils.isEmpty( specModelEntity.getModelName() ) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "模板名不能为空");
				return ;
			}
			if ( null == keys || keys.size() == 0 ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "规格不能为空");
				return ;
			}
			//模板名判重
			specModelEntity.setFacId(getCurrentFactoryUser().getFacId());
			if ( specModelService.judgeIsExist(specModelEntity) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "模板名称已存在");
				return ;
			}
			//规格判重
			if ( keys.size() > 1 ) {
				for (int i = 0; i < keys.size(); i++) {
					String goal = keys.get(i).getSpecName();
					for(int j=0; j<keys.size(); j++){
						if ( keys.get(j).getSpecName().equals(goal) && j != i ) {
							doResult(resp, false, StatusCode.SAVEERROR, null, "规格不能重复");
							return ;
						}
					}
				}
			}
			boolean flag = specModelService.insert(specModelEntity);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null, 
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
			return ;
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:编辑保存规格模板
	 * @param req
	 * @param resp
	 * @param specModel
	 * @author dingjiang
	 * @date 2017年9月12日上午9:14:54
	 */
	@RequestMapping(value="/modify",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void modify( HttpServletRequest req , HttpServletResponse resp , String specModel ){
		try {
			ProductSpecModelEntity specModelEntity = JSON.parseObject(StringEscapeUtils.unescapeHtml4(specModel), 
					ProductSpecModelEntity.class);
			List<ProductSpecKeyEntity> keys = specModelEntity.getSpecKeyEntities();
			//验证非空
			if ( StringUtils.isEmpty( specModelEntity.getModelName() ) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "模板名不能为空");
				return ;
			}
			//模板id为null,则为规格模板新增
			if ( LongUtils.isEmpty( specModelEntity.getId() ) ) {
				insert(req, resp, specModel);
			}
			//模板名判重
			specModelEntity.setFacId(getCurrentFactoryUser().getFacId());
			if ( specModelService.JudgeIsExist(specModelEntity) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "模板名称已存在");
				return ;
			}
			//规格判重
			if ( keys.size() > 1 ) {
				for (int i = 0; i < keys.size(); i++) {
					String goal = keys.get(i).getSpecName();
					for(int j=0; j<keys.size(); j++){
						String specName = keys.get(j).getSpecName();
						if ( !StringUtils.isEmpty(specName) ) {
							if ( specName.equals(goal) && j != i ) {
								doResult(resp, false, StatusCode.SAVEERROR, null, "规格不能重复");
								return ;
							}
						}
					}
				}
			}
			boolean flag = specModelService.modify(specModelEntity);
			doResult(resp, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, null, 
					flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
			return ;
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:批量修改
	 * @param req
	 * @param resp
	 * @param mid 模板ID
	 * @param id 规格ID
	 * @param str 规格值集合字符串
	 * @author dingjiang
	 * @date 2017年9月20日下午5:01:20
	 */
	@RequestMapping(value="/updateBatch",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void updateBatch( HttpServletRequest req, HttpServletResponse resp, Long mid, Long id, String str ){
		try {
			String[] strings = str.split(",");
			List<String> spec = new ArrayList<>();
			for (int i = 0; i < strings.length; i++) {
				spec.add(strings[i]);
			}
			specModelService.updateBatch(mid,id,spec);
			selectOne(req, resp, mid);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	
	/**
	 * Description:查询当前厂商的所有的模板
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年9月11日下午1:53:48
	 */
	@RequestMapping(value="/selectModels",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectModels( HttpServletRequest req , HttpServletResponse resp ){
		try {
			List<ProductSpecModelVo> find = specModelService.selectModels(getCurrentFactoryUser().getFacId());
			if ( find.size() == 0 ) {
				doResult(resp, true, StatusCode.FINDSUCCESS, null, ResultType.FIND_NULL);
				return ;
			}
			doResult(resp, true, StatusCode.FINDSUCCESS, find, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}	
	}
	
	/**
	 * @Description: 分页查询规格模板并级联查询规格键集合
	 * @param req
	 * @param resp
	 * @return void
	 * @author dingjiang
	 * @date 2017年8月7日下午2:42:38
	 * @throws 
	 */
	@RequestMapping(value="/selectpage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req , HttpServletResponse resp , 
			Integer page, Integer rows, String sort, String order){
		try {
			if (page == null) page = 1;
			if (rows == null) rows = 10;
			PageUtils<ProductSpecModelVo> pageinfo = new PageUtils<ProductSpecModelVo>(page, rows, sort, order);
			pageinfo.getCondition().put("sm.facId", getCurrentFactoryUser().getFacId());
			specModelService.selectforPage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:根据主键查询规格
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年9月7日下午5:52:00
	 */
	@RequestMapping(value="/selectOne",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectOne(HttpServletRequest req, HttpServletResponse resp, Long id ){
		try {
			if ( LongUtils.isEmpty(id) ) {
				doResult(resp, false, StatusCode.FINDERROR, null, "id不能为空");
				return ;
			}
			ProductSpecModelVo find = specModelService.find( id, getCurrentFactoryUser().getFacId() );
			if ( null == find ) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_NULL);
				return ;
			}
			doResult(resp, true, StatusCode.FINDSUCCESS, find, ResultType.FIND_SUCCESS);
			return ;
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	
	
	/**
	 * Description:批量删除规格模板
	 * @param req
	 * @param resp
	 * @param ids
	 * @author dingjiang
	 * @date 2017年9月7日下午5:24:14
	 */
	@RequestMapping(value="/delete",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void delete(HttpServletRequest req, HttpServletResponse resp, String ids){
		try {
			//将前台获取的商品ID字符串转为Long[]型数组
			String[] specIds = ids.split(",");
			Long[] arrays = new Long[specIds.length];
			for(int i = 0; i < specIds.length; i++){
				arrays[i] = Long.parseLong(specIds[i]);
		    }
			specModelService.deletes(getCurrentFactoryUser().getFacId(), arrays);
			doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.REMOVEERROR, null, e.getMessage());
		}
	}
	

	
	
	
	
	

	
	
	
}
